package com.cuitrip.util.time;

import com.cuitrip.service.R;
import com.cuitrip.util.I118Util;
import com.cuitrip.util.ResourceUtil;
import com.lab.rtenvironment.AppCoreRuntime;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by MiJack on 2015/12/4.
 */
public class OutputTime {
    String format;
    private TimeZone timeZone;

    public static OutputTime getLocalInstance(OutputType type) {
        return getInstance(type, I118Util.getTimeZone());
    }

    public static OutputTime getInstance(OutputType type, TimeZone timeZone) {
        OutputTime outputTime = new OutputTime();
        switch (type) {
            case Type_Minute:
                outputTime.format = AppCoreRuntime.context.getString(R.string.date_format_ymdhm);
                break;
            case Type_DAY:
                outputTime.format = AppCoreRuntime.context.getString(R.string.date_format_ymd);
                break;
            case TYPE_COMMON_DAY:
                outputTime.format = ResourceUtil.getString(R.string.COMMON_DAY_FORMAT);
                break;
            case TYPE_COMMON_ENGLISH:
                outputTime.format = ResourceUtil.getString(R.string.COMMON_ENGLISH_DAY_FORMAT);
                break;
            case TYPE_MONTH:
                outputTime.format = ResourceUtil.getString(R.string.date_format_ym);
                break;
            default:
                throw new RuntimeException(type.name() + " is not support ");
        }
        outputTime.timeZone = timeZone;
        return outputTime;
    }

    public enum OutputType {Type_DAY, Type_Minute, TYPE_COMMON_DAY, TYPE_MONTH, TYPE_COMMON_ENGLISH}


    public String format(long time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format, I118Util.getLocale());
        if (timeZone != null) {
            dateFormat.setTimeZone(timeZone);
        }
        try {
            Date date = new Date(time);
            return dateFormat.format(date);
        } catch (Exception e) {
            return null;
        }
    }
}
